/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;
import BE.Usuario;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author CarLita
 */
public class UsuarioDao {
        Session session;

    public boolean guardar(Usuario u) {
        boolean ok = true;
        session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        try {
            tx.begin();
            session.saveOrUpdate(u);
            tx.commit();
        } catch (HibernateException he){
            tx.rollback();
            he.printStackTrace();
            ok=false;
        } finally {
            session.close();
        }
        return ok;
    }

    public Usuario buscarPorId(int idUsuario) {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Usuario usuario where usuario.idUsuario= ? ";
        Query query = session.createQuery(hql);
        query.setInteger(0, idUsuario);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            return (Usuario)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }
    
    public Usuario buscarPorLogin(String login) {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Usuario usuario where usuario.login= ? ";
        Query query = session.createQuery(hql);
        query.setString(0, login);
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();
            if (resultList.isEmpty()) return null;
            return (Usuario)resultList.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }

    public List buscarPorCriterios(Usuario u) {
        session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Usuario usuario where usuario.nombres like concat('%',?,'%') "
                +"and usuario.apellidos like concat('%',?,'%') "
                + "and usuario.login like concat('%',?,'%') "
                + "and usuario.codigoTrabajador like concat('%',?,'%')";
        
        Query query = session.createQuery(hql);
        query.setString(0, u.getNombres());
        query.setString(1, u.getApellidos());
        query.setString(2, u.getLogin());
        query.setString(3, u.getCodigoTrabajador());
        List resultList = null;

        try {
            session.beginTransaction();
            resultList = query.list();
            session.getTransaction().commit();
            session.close();            
            return resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }

        session.close();
        return null;
    }
    
    public boolean eliminar(int idUsuario){
        boolean ok =true;
        Usuario usuario = buscarPorId(idUsuario);
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = session.beginTransaction();
            session.delete(usuario);
            tx.commit();
            session.close();
        }
        catch(Exception e){
            ok = false;
            System.out.println(e.getMessage());
        }
        return ok;
    }
}
